package thong.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.jsp.jstl.sql.Result;

import com.sun.mail.iap.Response;

import thong.util.JdbcUtil;

/**
 * Servlet implementation class CheckLogin
 */
@WebServlet("/CheckLogin")
public class CheckLogin extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CheckLogin() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		
		String id = request.getParameter("txtUserName");
		String pass = request.getParameter("txtPassword");
		
		HttpSession MySession = null;
          	 
			String sql = "SELECT * FROM Customers where Id = ? and Password = ?";
	        Result user = JdbcUtil.toResult(sql,id,pass);
	        if(user.getRowCount() <=0 )
	        {
	        	response.sendRedirect("ShowNotice.jsp");
	        }
	        else
	        {
	        	MySession = request.getSession();
	        	MySession.setAttribute("UserName", id);
	        	response.sendRedirect("index.jsp");
		       // request.getRequestDispatcher("ShowNotice.jsp").forward(request, response);
		        //request.setAttribute("UserName", id);
	        }
        
        /*
         * Chuyển tiếp sang trang Product.jsp
         */
       
	}

}
